package com.huawei.faketaobao.http

import com.huawei.fakedouyin.http.LogPrintInterceptor
import com.huawei.fakedouyin.http.SslUtil
import com.huawei.faketaobao.constant.IConstant
import okhttp3.OkHttpClient
import org.apache.http.conn.ssl.AllowAllHostnameVerifier
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * 网络请求封装
 */
class RetrofitConfig private constructor() {

    companion object {
        val Instantce by lazy { RetrofitConfig() }
    }

    private val okHttpClient = OkHttpClient.Builder()
        .readTimeout(IConstant.TimeOut, TimeUnit.MILLISECONDS)
        .writeTimeout(IConstant.TimeOut, TimeUnit.MILLISECONDS)
        .connectTimeout(IConstant.TimeOut, TimeUnit.MILLISECONDS)
        .retryOnConnectionFailure(true)
        .addInterceptor(LogPrintInterceptor())
        .sslSocketFactory(
            SslUtil.getSslContextForCertificateFile().socketFactory,
            SslUtil.getTrustManager()
        )
        .hostnameVerifier { _, _ -> true }
        .hostnameVerifier(AllowAllHostnameVerifier())
        .build()

    private val retrofit = Retrofit.Builder()
        .client(okHttpClient)
        .baseUrl(IConstant.PostUrl)
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    val api = retrofit.create(RequestService::class.java)
}